package com.trial.eyefollowupapp.screens

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.unit.*
import androidx.navigation.NavHostController
import com.trial.eyefollowupapp.theme.AppAnimations
import com.trial.eyefollowupapp.theme.AppDimens
import androidx.compose.foundation.background
import androidx.compose.animation.core.tween
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.shape.RoundedCornerShape

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(navController: NavHostController, userName: String) {
    var showContent by remember { mutableStateOf(false) }

    // 初始动画效果
    LaunchedEffect(Unit) {
        showContent = true
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("欢迎，$userName") },
                actions = {
                    TextButton(
                        onClick = {
                            navController.navigate("login") {
                                popUpTo("home/$userName") { inclusive = true }
                            }
                        },
                        colors = ButtonDefaults.textButtonColors(
                            contentColor = MaterialTheme.colorScheme.onPrimary
                        )
                    ) {
                        Text("退出")
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primary
                )
            )
        },
        contentWindowInsets = WindowInsets(0, 0, 0, 0) // 修复错误，使用正确的WindowInsets
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(it)
                .padding(horizontal = AppDimens.spacingXLarge),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            AnimatedVisibility(
                visible = showContent,
                enter = fadeIn(
                    animationSpec = tween(durationMillis = AppAnimations.durationMedium)
                ),
                exit = fadeOut(
                    animationSpec = tween(durationMillis = AppAnimations.durationMedium)
                )
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(AppDimens.spacingLarge)
                ) {
                    Text(
                        "您好，$userName 👋",
                        style = MaterialTheme.typography.headlineLarge,
                        color = MaterialTheme.colorScheme.onBackground
                    )

                    Spacer(modifier = Modifier.height(AppDimens.spacingXLarge))

                    AnimatedVisibility(
                        visible = showContent,
                        enter = scaleIn(
                            initialScale = 0.9f,
                            animationSpec = tween(durationMillis = AppAnimations.durationMedium)
                        ),
                        exit = scaleOut(
                            targetScale = 0.9f,
                            animationSpec = tween(durationMillis = AppAnimations.durationMedium)
                        )
                    ) {
                        Button(
                            onClick = {
                                navController.navigate("followUp/$userName")
                            },
                            shape = RoundedCornerShape(AppDimens.cornerRadiusMedium),
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = AppDimens.spacingLarge),
                            elevation = ButtonDefaults.buttonElevation(
                                defaultElevation = AppDimens.elevationMedium,
                                pressedElevation = AppDimens.elevationLarge
                            )
                        ) {
                            Text(
                                "查看随访记录",
                                fontSize = 16.sp,
                                modifier = Modifier.padding(vertical = AppDimens.spacingMedium)
                            )
                        }
                    }
                }
            }
        }
    }
}
